const app = getApp();
import {request} from '../../utils/request'
import config from '../../utils/config'
import {add,subtract,multiply,divide} from '../../utils/util'
Page({
    data: {
        custom: wx.getMenuButtonBoundingClientRect(),
        picUrl:config.picUrl,
        user:{},
        list:[],
        hideGuanli:true,
        checkAll:true,
        keyword:'',
        total:0.00
    },
    onLoad(options) {
        var user = wx.getStorageSync('userInfo')
        this.setData({ user})
        this.getList()
    },
    // onShow() {
    //     if (typeof this.getTabBar === 'function' && this.getTabBar()) {
    //         this.getTabBar().setData({
    //             selected: 3
    //         })
    //     }
    //     this.getList()
    //     if(!this.data.user){
    //         var timer = setInterval(()=>{
    //             console.log(timer)
    //             var userInfo = wx.getStorageSync('userInfo')
    //             if(userInfo){
    //                 this.setData({user:userInfo})
    //                 clearInterval(timer)
    //             }
    //         },1000)
    //     }
    // },
    getList(){
        request({
            url:"/miniprogram/user/getMyCart",
            data:{user_id:this.data.user.id,keyword:this.data.keyword}
        }).then(res=>{
            this.setData({list:res.data.list})
            this.check()
            this.compute()
        })
    },
    changeValue(e){
        this.setData({keyword:e.detail.value})
    },
    check(){ //检查是不是全选中了
        var list = this.data.list,
            checkAll = true
        for(let v of list){
            for(let v2 of v.good){
                if(v2.checked == 0){
                    checkAll = false
                }
            }
        }
        this.setData({checkAll})
    },
    showDel(){
        this.setData({hideGuanli:!this.data.hideGuanli})
    },
    choose(e){
        var {i1,i2} = e.currentTarget.dataset
        var list = this.data.list
        list[i1].good[i2].checked = list[i1].good[i2].checked == 1 ? 0 : 1
        this.setData({list})
        this.check()
        this.compute()
    },
    checkAll(){
        var checkall = this.data.checkAll
        var list = this.data.list
        for(let i1 in list){
            for(let i2 in list[i1].good){
                list[i1].good[i2].checked = checkall ? 0 : 1
            }
        }
        this.setData({list})
        this.check()
        this.compute()
    },
    compute(){
        var list = this.data.list,
            total = 0.00
        for(let v1 of list){
            for(let v2 of v1.good){
                if(v2.checked == 1){
                    var sub_total = add(multiply(v2.discount_price,v2.num,2),v2.freight,2)
                    total = add(total,sub_total,2)
                }
            }
        }
        this.setData({total})
    },
    getCheckedGood(){
        var list = this.data.list
        var id = []
        for(let v1 of list){
            for(let v2 of v1.good){
                if(v2.checked == 1){
                    id.push(v2.id)
                }
            }
        }
        return id;
    },
    delGood(){
        var id = this.getCheckedGood()
        wx.showModal({
            title:'真的要删除选中的剧本吗？',
            success:rel=>{
                if(rel.confirm){
                    request({url:"/miniprogram/user/delCart",data:{id},type:'post'}).then(res=>{
                        config.toast(res.msg,res.code,()=>{
                            this.getList()
                        })
                    })
                }
            }
        })

    },
    payGood(){
        var id = this.getCheckedGood().join('-')
        wx.navigateTo({url:"/pages/create_order/create_order?cart_id="+id})
    }
})